package org.linlinjava.litemall.core.exception;

/**
 * @author: Destin
 * @date: 2021-12-31
 */
public class BusinessException extends AbstractCoderException{
    public static final int ERROR_TYPE = 2;

    public BusinessException(String message) {
        super(ErrorCode.SystemDefaultCode.BUSINESS_EXCEPTION, message, 2);
    }

    private BusinessException(ErrorCode code) {
        super(code, 2);
    }

    private BusinessException(ErrorCode code, String message) {
        super(code, message, 2);
    }

    private BusinessException(ErrorCode code, String message, Throwable ex) {
        super(code, message, ex, 2);
    }

    public static BusinessException create(String message) {
        return create(ErrorCode.SystemDefaultCode.BUSINESS_EXCEPTION, message);
    }

    public static BusinessException create(ErrorCode errorCode) {
        return new BusinessException(errorCode);
    }

    public static BusinessException create(ErrorCode errorCode, String message) {
        return null == message ? create(errorCode) : new BusinessException(errorCode, message);
    }

    public static BusinessException create(ErrorCode errorCode, String message, Throwable exception) {
        if (null == exception) {
            return create(errorCode, message);
        } else if (exception instanceof BusinessException) {
            BusinessException source = (BusinessException) exception;
            return null != errorCode && errorCode != source.getErrorCode() ? new BusinessException(errorCode, message, source) : source;
        } else {
            return new BusinessException(errorCode, message, exception);
        }
    }
}
